def selection = (textArea.selection?.size() > 0)?textArea.selection[0].clone():null
def logic = { text ->
    def i = 0
    text.readLines().collect{
        (i++).toString().padLeft(2) + "." + it
    }.join("\n") + "\n"
}
if( selection ){
    textArea.selectedText = logic(textArea.selectedText)
    textArea.addToSelection(selection)
} else {
    textArea.text = logic(textArea.text)
}
